package com.etc.yzrnx.library.dao.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.etc.yzrnx.library.bean.Borrow_bookinfo;
import com.etc.yzrnx.library.dao.IBaseDao;
import com.etc.yzrnx.library.exception.MyException;
import com.etc.yzrnx.library.utils.Contacts;
import com.etc.yzrnx.library.utils.JDBCTools;

public class BaseDao<T> extends JDBCTools implements IBaseDao<T> {

	@Override
	public boolean add(T t, String sql) throws MyException {

		List<T> list = new ArrayList<>();
		list.add(t);
		try {
			int flag = excuteUpdate(sql, list);
			if (flag > 0) {
				return true;
			}
		} catch (Exception e) {

			throw new MyException(Contacts.MYEXCEPTIION_CONNECTION_ERRROR);
		}

		return false;
	}

	@Override
	public int update(T t, String sql) throws MyException {

		List<T> list = new ArrayList<>();
		list.add(t);
		try {
			return excuteUpdate(sql, list);
		} catch (Exception e) {
			throw new MyException(Contacts.MYEXCEPTIION_CONNECTION_ERRROR);
		}
		
	}
	
	@Override
	public boolean delete(String sql) throws MyException {
		
		try {
			return executeUpdate(sql, null);
		} catch (Exception e) {
			throw new MyException(Contacts.MYEXCEPTIION_CONNECTION_ERRROR);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryListAll(String sql, Object[] obj, Class<?> clazz)
			throws MyException {
		try {
			List<T>  list=executeQuery(sql, obj, clazz);
			return list;
		} catch (Exception e) {
			throw new MyException(Contacts.MYEXCEPTIION_CONNECTION_ERRROR);
		}
	}

	@Override
	public boolean save(T entity) throws MyException {
		// TODO Auto-generated method stub
		Class<?> clazz=entity.getClass();
		String dbTableName=clazz.getName();
		/**
		 * get class all field
		 */
		Field[] filed=clazz.getFields();
		
		return false;
	}

	@Override
	public boolean update(T entity) throws MyException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(T entity) throws MyException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List query(String sql, Object[] obj, Class<?> clazz)
			throws MyException {
		// TODO Auto-generated method stub
		try {
			return executeQuery(sql, obj, clazz);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new  MyException(e.getMessage());
		}
	}
}
	
